package com.jprocms.module.cms.config;

import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Configuration
public class OpenApiConfig {

    @Bean
    public OpenApiCustomiser openApiPrefixCustomizer() {
        return openApi -> {
            final Paths paths = openApi.getPaths();
            Map<String, PathItem> pathItemMap = new HashMap<>();
            paths.forEach((path, item) -> {
                pathItemMap.put(path, item);
            });
            final Set<String> pathKeys = pathItemMap.keySet();
            for (String pathKey : pathKeys) {
                paths.remove(pathKey);
                paths.addPathItem("/portal" + pathKey, pathItemMap.get(pathKey));
            }
        };
    }
}